package com.lims.utils;

import lombok.extern.slf4j.Slf4j;
import org.junit.platform.commons.util.StringUtils;

import java.util.regex.Pattern;

/**
 * @author sachin
 * @since 2020-07-03 16:23
 */
@Slf4j
public class ValidateUtils {

    private static final Pattern PHONE_PATTERN = Pattern.compile("^[1]([3-9])[0-9]{9}$");

    private static final Pattern URL_PATTERN = Pattern.compile("(http|ftp|https):\\/\\/[\\w\\-_]+(\\.[\\w\\-_]+)+([\\w\\-\\.,@?^=%&amp;:/~\\+#]*[\\w\\-\\@?^=%&amp;/~\\+#])?");

    private static final Pattern NAME_PATTERN = Pattern.compile("^[\\u4e00-\\u9fa5a-zA-Z0-9.·]+$");

    private static final Pattern MAIL_PATTERN = Pattern.compile("^([a-z0-9A-Z]+[-|_|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$");

    private static final Pattern ACCESS_CARD_NO_NUMBER = Pattern.compile("^\\d{6,10}$");

    public static final String PHONE_ERR_MSG = "请填写正确的手机号";

    public static final String PHONE_IMPORT_ERR_MSG = "手机号码格式不正确，请输入正确格式的手机号";

    public static final String NAME_ERR_MSG = "请填写正确的姓名";

    public static final String NAME_IMPORT_ERR_MSG = "姓名含有特殊字符，请输入无特殊字符姓名";

    public static final String MAIL_ERR_MSG = "请填写正确的邮箱";

    public static final String MAIL_IMPORT_ERR_MSG = "请填写正确的邮箱";

    public static final String ACCESS_CARD_NO_ERR_MSG = "门禁卡号只能输入6-10个数字";

    public static boolean isTel(String tel) {
        return isMatch(PHONE_PATTERN, tel);
    }

    public static boolean isUrl(String url) {
        return isMatch(URL_PATTERN, url);
    }

    public static boolean isName(String name) {
        return isMatch(NAME_PATTERN, name);
    }

    public static boolean isMail(String mail) {
        return isMatch(MAIL_PATTERN, mail);
    }

    public static boolean isAccessCardNo(String accessCardNo) {
        return isMatch(ACCESS_CARD_NO_NUMBER, accessCardNo);
    }

    public static boolean isMatch(Pattern pattern, String str) {
        return StringUtils.isNotBlank(str) && pattern.matcher(str).matches();
    }
}
